package com.fenrirtec.aepp.common.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fenrirtec.aepp.common.condition.MemberCondition;
import com.fenrirtec.aepp.common.dao.TenderBidrcordDao;
import com.fenrirtec.aepp.common.dao.impl.TenderBidrcordDao_JDBCImpl;
import com.fenrirtec.aepp.common.dto.TenderBidrcordListDto;
import com.fenrirtec.aepp.common.model.TenderBidrcordList;
import com.fenrirtec.aepp.common.service.TenderBidrcordService;
import com.fenrirtec.core.database.DatabaseSessionManager;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.BeanCopierUtils;

public class TenderBidrcordServiceImpl implements TenderBidrcordService{
	
	private static final Logger logger = LoggerFactory.getLogger(TenderServiceImpl.class);

    @Override
    public List<TenderBidrcordListDto> searchByCnd(MemberCondition condition) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderBidrcordServiceImpl#searchByCnd] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<TenderBidrcordList> tenderBidrcordList = null;

        try {
            connection.setAutoCommit(false);
            TenderBidrcordDao tenderBidrcordDao = new TenderBidrcordDao_JDBCImpl();
            tenderBidrcordList = tenderBidrcordDao.searchByCnd(condition);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderBidrcordServiceImpl#searchByCnd] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderBidrcordServiceImpl#searchByCnd] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderBidrcordServiceImpl#searchByCnd] finish.");
            }
            sessionManager.close();
        }
        
        List<TenderBidrcordListDto> tenderBidrcordDtoList = null;
        
        if (tenderBidrcordList != null && tenderBidrcordList.size() > 0) {
        	tenderBidrcordDtoList = new ArrayList<TenderBidrcordListDto>();
            for (TenderBidrcordList tenderBidrcord : tenderBidrcordList) {
                TenderBidrcordListDto tenderBidrcordDto = new TenderBidrcordListDto();
                BeanCopierUtils.copyProperties(tenderBidrcord, tenderBidrcordDto);
                tenderBidrcordDtoList.add(tenderBidrcordDto);
            }
        }
        
        return tenderBidrcordDtoList;
    }
}
